package com.pasabahceambalaj.session;

import java.util.Arrays;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.framework.EntityQuery;

import com.pasabahceambalaj.entity.Firma;
import com.pasabahceambalaj.entity.Kullanici;
import com.pasabahceambalaj.entity.Rol;

@Name("rolList")
public class RolList extends EntityQuery<Rol> {

	private static final long serialVersionUID = 1L;

	private static final String EJBQL = "select rol from Rol rol";
	private static final String EJBQL_no_data = "select rol from Rol rol where rol.id=-1";

	private static final String[] RESTRICTIONS = { "lower(rol.ad) like lower(concat(#{rolList.rol.ad},'%'))", "lower(rol.kod) like lower(concat(#{rolList.rol.kod},'%'))", };

	private Rol rol = new Rol();

	public RolList() {
		if (Authenticator.isLoggedIn()) {
			if (!Authenticator.isAdmin()) {
				Kullanici user = (Kullanici) Contexts.getSessionContext().get("user");
				Firma firma = user.getFirma();
				setEjbql(EJBQL + " where rol.firma.id = " + firma.getId());
			} else {
				setEjbql(EJBQL);
			}
		} else {
			setEjbql(EJBQL_no_data);
		}
		setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
		setMaxResults(25);
	}

	public Rol getRol() {
		return rol;
	}
}
